package cn.edu.cqupt.mis.ywbd.admin.service.impl;

import cn.edu.cqupt.mis.ywbd.admin.dao.*;
import cn.edu.cqupt.mis.ywbd.admin.dto.ListQuestionDto;
import cn.edu.cqupt.mis.ywbd.admin.service.QuestionService;
import cn.edu.cqupt.mis.ywbd.admin.vo.AdminQuestionDetailVo;
import cn.edu.cqupt.mis.ywbd.admin.vo.QuestionListVo;
import cn.edu.cqupt.mis.ywbd.common.pojo.*;
import cn.edu.cqupt.mis.ywbd.common.vo.PageInfoVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 李国龙
 * @date 2021/4/7
 */
@Service
public class QuestionServiceImpl implements QuestionService {

    @Autowired
    QuestionMapper questionMapper;
    @Autowired
    CollectionMapper collectionMapper;
    @Autowired
    ReportMapper reportMapper;
    @Autowired
    AnswerMapper answerMapper;
    @Autowired
    SubjectMapper subjectMapper;
    @Autowired
    AccountMapper accountMapper;

    @Override
    public Map listQuestion(ListQuestionDto questionDto) {
        List<Object> subjectIds = subjectMapper.selectObjs(new QueryWrapper<Subject>()
                .like("subject_name", questionDto.getSubjectName())
                .like("college", questionDto.getCollege())
                .select("subject_id"));
        subjectIds.add("");
        List<Object> accountIds = accountMapper.selectObjs(new QueryWrapper<Account>()
                .like("user_name", questionDto.getQuestionAccountName())
                .select("account_id"));
        accountIds.add("");

        List<Object> answerAccountIds = accountMapper.selectObjs(new QueryWrapper<Account>()
                .like("user_name", questionDto.getAnswerAccountName())
                .select("account_id"));
        answerAccountIds.add("");
        List<Object> answerQuestionIds = answerMapper.selectObjs(new QueryWrapper<Answer>()
                .in("account_id", answerAccountIds)
                .select("question_id"));
        answerQuestionIds.add("");

        QueryWrapper<Question> in = new QueryWrapper<>();
        if (!StringUtils.isEmpty(questionDto.getSubjectName())||!StringUtils.isEmpty(questionDto.getCollege())) {
            in.in("subject_id",subjectIds);
        }
        if (!StringUtils.isEmpty(questionDto.getAnswerAccountName())) {
            in.in("question_id",answerQuestionIds);
        }
        if (!StringUtils.isEmpty(questionDto.getQuestionAccountName())) {
            in.in("account_id",accountIds);
        }
        in.like("title",questionDto.getTitle())
                .like("publish_time",questionDto.getTime())
                .like("state",questionDto.getState())
                .orderByDesc("publish_time");
        Page<Question> questionPage = questionMapper.selectPage(new Page<>(questionDto.getCurrentPage(), questionDto.getPageSize()), in);
        List<Question> questions = questionPage.getRecords();
        PageInfoVo pageInfoVo = new PageInfoVo();
        pageInfoVo.setCurrentPage((int) questionPage.getCurrent());
        pageInfoVo.setPageSize((int) questionPage.getSize());
        pageInfoVo.setTotalPages((int) questionPage.getPages());
        pageInfoVo.setTotalRows((int) questionPage.getTotal());
        Map<String, Object> result = new HashMap(2);
        result.put("pageInfo",pageInfoVo);
        result.put("list", questions);
        return result;
    }

    @Override
    public AdminQuestionDetailVo getQuestionById(Integer questionId) {
        AdminQuestionDetailVo adminQuestionDetailVo = new AdminQuestionDetailVo();
        Question question = questionMapper.selectOne(new QueryWrapper<Question>().eq("question_id", questionId));
        adminQuestionDetailVo.setQuestionId(question.getQuestionId());
        adminQuestionDetailVo.setTitle(question.getTitle());
        adminQuestionDetailVo.setDescribes(question.getDescribes());
        adminQuestionDetailVo.setPublishTime(question.getPublishTime());
        adminQuestionDetailVo.setUpdateTime(question.getUpdateTime());
        adminQuestionDetailVo.setImgPath(question.getImgPath());
        return adminQuestionDetailVo;
    }

    public List<QuestionListVo> castQuestionListToQuestionListVoList(List<Question> questions) {
        List<QuestionListVo> questionListVos = new ArrayList<>();
        for (Question question : questions) {
            QuestionListVo questionListVo = new QuestionListVo();
            Integer answerCount = answerMapper.selectCount(new QueryWrapper<Answer>().eq("question_id", question.getQuestionId()));
            Integer reportCount = reportMapper.selectCount(new QueryWrapper<Report>().eq("thing_id", question.getQuestionId()));
            Integer collectionCount = collectionMapper.selectCount(new QueryWrapper<Collection>().eq("question_id", question.getQuestionId()));
            Subject subject = subjectMapper.selectOne(new QueryWrapper<Subject>().eq("subject_id",question.getSubjectId()));
            String subjectName = subject.getSubjectName();
            String userName = accountMapper.selectOne(new QueryWrapper<Account>().eq("account_id",question.getAccountId())).getUserName();
            String college = subject.getCollege();
            questionListVo.setAnswerCount(answerCount);
            questionListVo.setCollectionCount(collectionCount);
            questionListVo.setReportCount(reportCount);
            questionListVo.setQuestionId(question.getQuestionId());
            questionListVo.setTitle(question.getTitle());
            questionListVo.setState(question.getState());
            questionListVo.setQuestionAccountId(question.getAccountId());
            questionListVo.setPublishTime(question.getPublishTime());
            questionListVo.setSubjectName(subjectName);
            questionListVo.setQuestionAccountName(userName);
            questionListVo.setCollege(college);
            questionListVos.add(questionListVo);
        }
        return questionListVos;
    }
}
